/**
 * Created by liebling on 2017/7/1.
 */
"use strict";
import $script from 'scriptjs';
import '../main';
import '../../css/userCenter/userManage.less';
import '../../css/parts/common.less';
import '../../../public/plugins/xui/xui.1.0.0.css';

var ApiService = require('../module/ApiService');

$script.ready('jqueryFile', function () {
	require('../component/common/header');
	require('../component/common/header2');
	require('../component/common/workNav');
	require('../component/common/list');
	require('../../../public/plugins/laydate/laydate');
	require('../../../public/plugins/jquery.validate');
	require('../../../public/plugins/jquery.validate.extend');
	let xui = require('../../../public/plugins/xui/xui.1.0.0');
	
	var index = layer.load(1, {shade: [1, '#fff'], scrollbar: false});
	var HTML = require('../../component/layer/createConf.html');
	avalon.filters.statusName = function (val) {
		switch (val){
			case  'disable':
				return '失效';
				break;
			case 'delete':
				return '删除';
				break;
			case 'user':
			default : 
				return '有效'
		}
	};
	var paramsData = [
		{
			name: 'systemConf',
			params: $.extend({
				name: '',
				status: '',
				pageNumber: 1,
				pageSize: 10,
			}, ApiService.getParams())
		},
	];
	var pageData = avalon.define({
		$id: 'pageData',
		data: {
			mallList: {},
			detail:{
				name:'',
				value:'',
				status:'used',
				statusName:'有效',
				memo:''
			},
		},
		statusList:[
			{value:'used',name:'有效'},
			{value:'disable',name:'失效'},
			{value:'delete',name:'删除'},
		],
		config: {
			mallList: {
				params: {
					name: '',
					status: '',
					pageNumber: 1,
					pageSize: 10,
				},
				filterParams: {},
				listName: 'data.results',
				post: 'systemConf'
			}
		},
		init: function () {
		},
		updateConfig: function (type,el) {
			var _this = this;
			if(type == 'systemConfUpdate') {
				_this.data.detail = el;
				_.set(_this.data.detail,'statusName',avalon.filters.statusName(el.status));
			}
			layer.open({
					title:'新建参数',
					content:HTML,
					btn:['保存','取消'],
					area:['500px'],
					success:function () {
						avalon.scan(document.body);
						xui.picker({
							'element': $('#statusName'),
							'value': _this.statusList,
							'onClose': function (val, element, selectCol) {
								$('#status').val(selectCol.data('value'));
							}
						});
						$('#createConf').validate({
							errorElement: "label",
							onkeyup: false,
							ignore: '.notCheck',
							errorPlacement: function (er, el) {
								$(el).removeClass("m-success-con");
								$(el).addClass("m-error-con");
								$(el).closest(".fn-item").find(".xui-wordError").append(er);
							},
							success: function (element, callblack) {
								$(element).remove();
								$(callblack).removeClass("m-error-con");
								$(callblack).addClass("m-success-con");
							},
							onfocusout: function (element) {
								$(element).valid();
							},
							rules: {
								name: {
									required: true
								},
								value: {
									required: true
								},
								status: {
									required: true,
								},
							},
							messages: {
								name: {
									required: '必填'
								},
								value: {
									required: '必填'
								},
								status: {
									required: '必填'
								},
							},
							submitHandler: function (from) {
								var params = ApiService.serialize($(from).find('input[name]:not(.ignore),select[name]:not(.ignore)'));
								if(type == 'systemConfUpdate') {
									_.set(params,'id',el._id);
								}
								var Index = layer.load(1, {shade: [0.3, '#000'], scrollbar: false});
								ApiService.postApi(type, params)
									.then(ApiService.error)
									.then(function (res) {
										layer.msg(res.message, {
											time: 2000
										}, function () {
											$('.list-view').trigger('refresh');
											layer.closeAll();
										})
									})
									.catch(function (err) {
										layer.close(Index);
									});
								return false;
							}
						});
					},
					yes:function (index,layero) {
						$(layero).find('form').submit();
						return false;
					},
				})
		},
		del: function (el) {
			var Index = layer.load(1, {shade: [0.3, '#000'], scrollbar: false});
			ApiService.postApi('systemConfDelete', {id: el._id})
				.then(ApiService.error)
				.then(function (res) {
					layer.msg(res.message, {
						time: 2000
					}, function () {
						$('.list-view').trigger('refresh');
						layer.close(Index);

					})
				})
				.catch(function (err) {
					layer.close(Index);
				});
		},
		//批量修改
		changeStatus: function (items, status) {
			var Index = layer.load(1, {shade: [0.3, '#000'], scrollbar: false});
			var params = {
				status: status,
				ids: _.compact(_.map(items, function (item) {
					if (item.status != status) {
						return item._id
					} else {
						return 0;
					}
				}))
			};
			if (_.size(params.ids)) {
				params.ids = params.ids.join(',');
				ApiService.postApi('systemConfUpdate', params)
					.then(ApiService.error)
					.then(function (res) {
						layer.msg(res.message, {
							time: 2000
						}, function () {
							$('.list-view').trigger('refresh');
							layer.close(Index);

						})
					})
					.catch(function (err) {
						layer.close(Index);
					});
			} else {
				layer.msg('选择数据已经是当前状态，请重新选择。', {}, function () {
					layer.close(Index);
				})
			}

		}
	});
	ApiService.addTaskList(paramsData)
		.then(function () {
			return ApiService.doTaskList();
		})
		.then(function (data) {
			pageData.data.mallList = data.systemConf;
			$('.list-view').trigger('reload');
			pageData.init();
			layer.close(index);
			return null;
		})
		.catch(function (error) {
		});
});
